Modify VisionAgent to fetch and send image bytes#3045
Modify VisionAgent to fetch and send image bytes#3045bingbing-gui wants to merge 1 commit intomicrosoft:mainfrom
Conversation
Updated image handling in VisionAgent to use byte array. System.Net.Http.HttpRequestException: 'Response status code does not indicate success: 403 (Forbidden).'
There was a problem hiding this comment.
Pull request overview
This PR modifies the VisionAgent sample to fetch image bytes using HttpClient instead of passing a URI directly to the API. This change resolves a 403 Forbidden error that occurs when some image hosting services (like Wikipedia) block direct URI access from automated requests.
- Changed image handling from URI-based (
UriContent) to byte array-based (DataContent) - Added HttpClient with User-Agent header to download image bytes before sending to the agent
- Maintains the same functional behavior while working around external service restrictions
|
|
||
|
|
||
| using HttpClient httpClient = new(); | ||
| httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64)"); |
There was a problem hiding this comment.
The User-Agent header is added to work around the 403 Forbidden error, but there's no comment explaining why this is necessary. According to the coding guidelines, sample code should be well-documented with comments explaining the purpose of each step. Consider adding a comment above line 22 explaining that some image hosting services (like Wikipedia) require a User-Agent header to prevent automated scraping.
| name: "VisionAgent", | ||
| instructions: "You are a helpful agent that can analyze images"); | ||
|
|
||
|
|
There was a problem hiding this comment.
There's an extra blank line here that should be removed to maintain consistency with the coding style in other samples.
Updated image handling in VisionAgent to use byte array.
System.Net.Http.HttpRequestException: 'Response status code does not indicate success: 403 (Forbidden).'
Motivation and Context
Description
Contribution Checklist